// by Xeno
private ["_create_bike", "_disp_name", "_str", "_pos", "_vehicle", "_exitit", "_dosearch", "_index", "_parray", "_rank"];
if (!X_Client) exitWith {};

_create_bike = (_this select 3) select 0;
_b_mode = (_this select 3) select 1;

_disp_name = [_create_bike,0] call XfGetDisplayName;

if (vehicle player != player) exitWith {
	_str = format [localize "MSG_DOM_CREATINGVECFAIL", _disp_name];
	_str call XfGlobalChat;
};

if (isNil {player GetVariable "d_bike_created"}) then { player setVariable["d_bike_created",False];};
if ((player GetVariable "d_bike_created") && _b_mode == 1) exitWith {localize "MSG_DOM_CREATINGVECONCE" call XfGlobalChat};

if (time > d_vec_end_time && !isNull d_flag_vec) then {
	if ((d_flag_vec call XfGetAliveCrew) == 0) then {
		deleteVehicle d_flag_vec;
		d_flag_vec = objNull;
		d_vec_end_time = -1;
	};
};
if (!isNull d_flag_vec && alive d_flag_vec && _b_mode == 0) exitWith {
	(format [localize "MSG_DOM_CREATINGVECTOOSOON",0 max (round((d_vec_end_time - time)/60))]) call XfGlobalChat;
};

_pos = position player;
_str = format [localize "MSG_DOM_CREATINGVECSTANDBY", _disp_name];
_str call XfGlobalChat;
sleep 3.123;
player setVariable ["d_bike_created", true];
_pos = position player;
_vehicle = createVehicle [_create_bike, _pos, [], 0, "NONE"];
_vehicle setDir direction player;
player reveal _vehicle;

player moveInDriver _vehicle;

if (_b_mode == 1) then {
	_vehicle spawn {
		private "_vehicle";
		_vehicle = _this;
		["d_ad", _vehicle] call XNetCallEvent;
		waitUntil {sleep 0.412;!alive player || !alive _vehicle};
		sleep 10.123;
		_vehicle spawn {
			private "_vehicle";
			_vehicle = _this;
			while {true} do {
				if ((_vehicle call XfGetAliveCrew) == 0) exitWith {deleteVehicle _vehicle};
				sleep 15.123;
			};
		};
	};
} else {
	d_flag_vec = _vehicle;
	d_vec_end_time = time + VecCreateWaitTime + 60;
	["d_ad2", [d_flag_vec,d_vec_end_time]] call XNetCallEvent;
	d_flag_vec addEventHandler ["killed", {(_this select 0) spawn {private ["_vec"];_vec = _this;sleep 10.123;while {true} do {if (isNull _vec) exitWith {};if ((_vec call XfGetAliveCrew) == 0) exitWith {deleteVehicle _vec};sleep 15.123};d_flag_vec = objNull}}];
};
